package com.bristua.barcode.common.util;

import com.bristua.barcode.common.bean.CustomerParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Administrator on 2017/10/17 0017.
 */
@Component
public class HttpSessionUtil {

    private final static String TOKEN_KEY = "token";

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    @Autowired
    private HttpServletRequest request;


    @Autowired
    private HttpServletResponse response;

    public HttpServletResponse getResponse() {
        return response;
    }

    public String getToken() {
        return request.getHeader(TOKEN_KEY);
    }


    public CustomerParams getUserInfo() {
        String token = getToken();
        String userInfo = stringRedisTemplate.opsForValue().get(token);
        if (StringUtils.isEmpty(userInfo)) {
            return null;
        }
        return JsonUtil.json2Object(userInfo, CustomerParams.class);
    }


    public String getUserId() {
        String token = getToken();
        String userId = stringRedisTemplate.opsForValue().get(token);
        if (StringUtils.isEmpty(userId)) {
            return null;
        }
        return userId;
    }

}
